﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iFinity.DNN.Modules.UrlRedirectProvider.Entities
{
    internal class RedirectInfo
    {
     
        public Guid RedirectId {get;set;}
		public int PortalId {get;set;}
		public int RedirectOrder {get;set;}
		public string HttpAlias {get;set;}
		public string RedirectUrl {get;set;}
		public string MatchType {get;set;}
		public bool KeepQueryString {get;set;}
		public string DestTabPath {get;set;}
		public int DestTabId {get;set;}
		public string DestUrl {get;set;}
		public string DestType {get;set;}
		public bool IsRegex {get;set;}

        //derived field, returns the url as generated by the matchType
        internal string MatchUrl
        {
            get
            {
                return GetUrl(MatchType, HttpAlias, RedirectUrl);
            }
        }
        internal static string GetUrl(string matchType, string httpAlias, string redirectUrl)
        {
            string url = null;
            switch (matchType.ToLower())
            {
                case "relative":
                    url= redirectUrl;
                    break;
                case "absolute":
                    if (string.IsNullOrEmpty(httpAlias))
                        return redirectUrl;
                    else
                    {
                        url = httpAlias;
                        if (url.EndsWith("/") == false)
                            url += "/";
                        if (redirectUrl.StartsWith("/") && redirectUrl.Length > 0)
                            url += redirectUrl.Substring(1);
                        else
                            url += redirectUrl;
                    }
                    break;
            }
            return url;
        }
    }
}
